package com.yanqu.road.dao.impl.activity.fivehero;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroMatchData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFiveHeroMatchDataDaoImpl extends TempDataObjectDao<CrossFiveHeroMatchData> {

    public static String getCrossFiveHeroDataSql = "select * from `t_c_activity_fivehero_match_data` " +
            "where `activityId`=? AND groupId = ?";

    public Map<Integer,CrossFiveHeroMatchData> getCrossFiveHeroMatchData(int activityId,int groupId) {
        Map<Integer,CrossFiveHeroMatchData> resMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFiveHeroDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            if (rs.next()) {
                CrossFiveHeroMatchData data = parse(rs);
                resMap.put(data.getMatchId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resMap;
    }

    public static String updateCrossFiveHeroDataSql = "update `t_c_activity_fivehero_match_data` set " +
            "expMap=?,expLevelMap=?,scoreMap=?,scoreTime=?,scoreCityMap=?,scoreTokenMap=?,popMap=?" +
            " where activityId=? AND matchId = ? AND groupId = ?";

    @Override
    protected boolean updateData(CrossFiveHeroMatchData crossFiveHeroMatchData) {
        crossFiveHeroMatchData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getExpMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getExpLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getScoreMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossFiveHeroMatchData.getScoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getScoreCityMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getScoreTokenMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getPopMap())));

        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroMatchData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroMatchData.getMatchId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroMatchData.getGroupId()));
        if (getDb().executeNoneQuery(updateCrossFiveHeroDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossFiveHeroDataSql = "insert into `t_c_activity_fivehero_match_data`" +
            "(activityId,expMap,matchId,expLevelMap,scoreMap,scoreTime,scoreCityMap,scoreTokenMap,groupId,popMap)" +
            " values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossFiveHeroMatchData crossFiveHeroMatchData) {
        crossFiveHeroMatchData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroMatchData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getExpMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroMatchData.getMatchId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getExpLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getScoreMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossFiveHeroMatchData.getScoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getScoreCityMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getScoreTokenMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroMatchData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroMatchData.getPopMap())));


        if (getDb().executeNoneQuery(insertCrossFiveHeroDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossFiveHeroMatchData parseData(ResultSet rs) throws SQLException {
        CrossFiveHeroMatchData data = new CrossFiveHeroMatchData();
        data.setGroupId(rs.getInt("groupId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setExpMap(JSON.parseObject(rs.getString("expMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
        data.setMatchId(rs.getInt("matchId"));
        data.setExpLevelMap(JSON.parseObject(rs.getString("expLevelMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
        data.setScoreMap(JSON.parseObject(rs.getString("scoreMap"), new TypeReference<ConcurrentHashMap<Integer, Long>>() {
        }));

        if(!StringUtils.isNullOrEmpty(rs.getString("scoreCityMap"))){
            Map<Integer, Map<Integer, Long>> aaMap = new ConcurrentHashMap<>();
            Map<Integer, Map<Integer, Long>> scoreCityMap = JSON.parseObject(rs.getString("scoreCityMap"), new TypeReference<ConcurrentHashMap<Integer, Map<Integer,Long>>>() {
            });
            for(int a : scoreCityMap.keySet()){
                aaMap.put(a,new ConcurrentHashMap<>(scoreCityMap.get(a)));
            }
            data.setScoreCityMap(aaMap);
        }
        if(!StringUtils.isNullOrEmpty(rs.getString("scoreTokenMap"))){
            Map<Integer, Map<Integer, Long>> aaMap = new ConcurrentHashMap<>();
            Map<Integer, Map<Integer, Long>> scoreTokenMap = JSON.parseObject(rs.getString("scoreTokenMap"), new TypeReference<ConcurrentHashMap<Integer, Map<Integer, Long>>>() {
            });
            for(int a : scoreTokenMap.keySet()){
                aaMap.put(a,new ConcurrentHashMap<>(scoreTokenMap.get(a)));
            }
            data.setScoreTokenMap(aaMap);
        }
        if(!StringUtils.isNullOrEmpty(rs.getString("popMap"))){
            Map<Integer, Map<Integer, Long>> aaMap = new ConcurrentHashMap<>();
            Map<Integer, Map<Integer, Long>> scoreTokenMap = JSON.parseObject(rs.getString("popMap"), new TypeReference<ConcurrentHashMap<Integer, Map<Integer, Long>>>() {
            });
            for(int a : scoreTokenMap.keySet()){
                aaMap.put(a,new ConcurrentHashMap<>(scoreTokenMap.get(a)));
            }
            data.setPopMap(aaMap);
        }

        data.setScoreTime(rs.getLong("scoreTime"));
        return data;
    }

}
